﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SMM.MainForm
{
    public partial class AdminForm : Form
    {
        sqlHelper helper = new sqlHelper();
        public AdminForm()
        {
            InitializeComponent();
            loadDGV();
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            LoginForm f = new LoginForm();
            f.Show();
            this.Close();
        }

        private void loadDGV() {
            dgvShowMen.DataSource = helper.getTable("cashier", "cargoclerk").Tables[0];
            dgvShowMen.Columns[0].HeaderText = "ID";
            dgvShowMen.Columns[1].HeaderText = "姓名";
            dgvShowMen.Columns[2].HeaderText = "状态";

            dgvCargo.DataSource = helper.getTable("cargo").Tables[0];
            dgvCargo.Columns[0].HeaderText = "货物ID";
            dgvCargo.Columns[1].HeaderText = "货物条形码";
            dgvCargo.Columns[2].HeaderText = "货物名称";
            dgvCargo.Columns[3].HeaderText = "货物类型";
            dgvCargo.Columns[4].HeaderText = "货物价格";
            dgvCargo.Columns[5].HeaderText = "货物库存";
            dgvCargo.Columns[6].HeaderText = "货物预警库存";
        }

        private void dgvShowMen_MouseDown(object sender, MouseEventArgs e)
        {
            int index = dgvShowMen.CurrentRow.Index;
            menIDLabel.Text = dgvShowMen.Rows[index].Cells[0].Value+"";
            menNameTxt.Text = dgvShowMen.Rows[index].Cells[1].Value + "";
            if ((dgvShowMen.Rows[index].Cells[0].Value + "").Substring(0, 1) == "C")
            {
                menRoteTxt.Text = "收银员";
            }
            else
                menRoteTxt.Text = "货物员";
            if ((dgvShowMen.Rows[index].Cells[2].Value + "") == "1")
            {
                menStateTxt.Text = "正常";
            }
            else
                menStateTxt.Text = "禁用";
        }

        private void menSubBtn_Click(object sender, EventArgs e)
        {
            int state = menStateTxt.Text == "正常" ? 1:-1;
            bool flag = false;
            if (menIDLabel.Text.Substring(0, 1) == "C") {
                flag=helper.updateMen("cashier",menIDLabel.Text,menNameTxt.Text,state);
            }else
                flag=helper.updateMen("cargoclerk", menIDLabel.Text, menNameTxt.Text, state);
            if (flag)
                MessageBox.Show("修改成功！","成功");
            else
                MessageBox.Show("修改失败！", "错误");
            loadDGV();
        }

        private void dgvCargo_MouseClick(object sender, MouseEventArgs e)
        {
            int index = dgvCargo.CurrentRow.Index;
            cargoIDLab.Text=dgvCargo.Rows[index].Cells[0].Value + "";
            cargoNameTxt.Text= dgvCargo.Rows[index].Cells[2].Value + "";
            cargoBarTxt.Text= dgvCargo.Rows[index].Cells[1].Value + "";
            cargoTypeTxt.Text= dgvCargo.Rows[index].Cells[3].Value + "";
            cargoPriceTxt.Text= dgvCargo.Rows[index].Cells[4].Value + "";
            cargoNumTxt.Text= dgvCargo.Rows[index].Cells[5].Value + "";
            cargoMinNumTxt.Text= dgvCargo.Rows[index].Cells[6].Value + "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool flag =helper.updateCargo(cargoIDLab.Text, cargoBarTxt.Text,cargoNameTxt.Text,cargoTypeTxt.Text,float.Parse(cargoPriceTxt.Text.Trim()),int.Parse(cargoNumTxt.Text.Trim()),int.Parse(cargoMinNumTxt.Text.Trim()));
            if (flag)
                MessageBox.Show("修改成功！", "成功");
            else
                MessageBox.Show("修改失败！", "错误");
            loadDGV();
        }
    }
}
